package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jd.finance.core.mapper.BankMapper;
import com.jd.finance.core.mapper.ProductMapper;
import com.jd.finance.core.pojo.entity.Product;
import com.jd.finance.core.pojo.entity.Rate;
import com.jd.finance.core.mapper.RateMapper;
import com.jd.finance.core.service.OpreateLogService;
import com.jd.finance.core.service.RateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.IpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * <p>
 * 利率表，产品，银行中间表 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-20
 */
@Service
public class RateServiceImpl extends ServiceImpl<RateMapper, Rate> implements RateService {

    @Resource
    private AdminUtilsServiceImpl adminUtilsService;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private BankMapper bankMapper;
    @Resource
    private RateMapper rateMapper;
    @Resource
    private OpreateLogService opreateLogService;


    /**
     * 添加贷款产品
     *
     * @param uid
     * @param rate
     * @param money
     * @param proName
     * @param proInfo
     * @param repayWay
     * @param request
     * @return
     */
    @Override
    public BaseResult addLoanProduct(Integer uid, BigDecimal rate, BigDecimal money, String proName,
                                     String proInfo, String proKey, String repayWay, HttpServletRequest request) {


        if (uid == null || rate == null || money == null) {
            log.error("参数为空S");
            return BaseResult.error().message("参数为空");
        }
        if (StringUtils.isAllBlank(proName)) {
            log.error("添加贷款产品,参数为空S");
            return BaseResult.error().message("参数为空");
        }
        if (!adminUtilsService.isBank(uid)) {
            log.error("无访问权限S");
            return BaseResult.error().message("无访问权限");
        }
        //查询产品名判断是否存在
        QueryWrapper<Product> qwp = new QueryWrapper<>();
        qwp.lambda().eq(Product::getProName, proName);
        Product product = productMapper.selectOne(qwp);

        //产品存在
        if (product != null) {
            Rate r = new Rate();
            r.setPid(product.getPid());
            int i = rateMapper.insert(r);
            boolean add = opreateLogService.add(uid, "添加", "添加贷款产品", IpUtil.getIpAddr(request));
            if (!add) {
                log.error("操作日志添加失败");
                return BaseResult.error().message("操作日志添加失败");
            }
            return BaseResult.ok().data("tianjiajg", i);
        }
        //产品不存在
        Product p = new Product();
        p.setProName(proName);
        p.setProInfo(proInfo);
        p.setProKey(proKey);
        p.setRepayWay(repayWay);
        Rate ra = new Rate();
        ra.setPid(p.getPid());
        ra.setRate(rate);
        ra.setMoney(money);
        int updatePro = productMapper.insert(p);
        int updateRa = rateMapper.insert(ra);
        //添加操作日志
        boolean add = opreateLogService.add(uid, "添加", "添加信贷产品", IpUtil.getIpAddr(request));
        if (!add) {
            log.error("操作日志添加失败");
            return BaseResult.error().message("操作日志添加失败");
        }
        if (updatePro ==0 || updateRa ==0 ){
            log.error("添加失败S");
            return BaseResult.error().message("添加失败");
        }
        return BaseResult.ok().data("tianjiajg",0);
    }
}

